package com.org.controller;

import com.org.common.PageBean;
import com.org.common.Result;
import com.org.pojo.*;
import com.org.service.CommunityAdminService;
import com.org.utils.JwtUtil;
import com.org.utils.Md5Util;
import com.org.utils.ThreadLocalUtil;
import org.hibernate.validator.constraints.URL;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * ClassName: CommunityUserController
 * Package: com.org.controller
 * Description:
 *
 * @Author Origin
 * @Create 2024/2/3 0:02
 * @Version 1.0
 */

@RestController
@RequestMapping("/communityAdmin")
@CrossOrigin//支持跨域
public class CommunityAdminController {

    @Autowired
    private CommunityAdminService communityAdminService;

    @PostMapping("/register")
    public Result register(@RequestBody CommunityAdmin communityAdmin) {
        CommunityAdmin c = communityAdminService.findByUsername(communityAdmin.getUsername());
        if (c == null) {
            //没有占用  注册
            //加密
            //communityAdmin.setPassword(Md5Util.getMD5String(communityAdmin.getPassword()));
            communityAdmin.setUserType(0);
            communityAdmin.setState("待审核");
            //添加
            communityAdminService.save(communityAdmin);
            return Result.success(communityAdmin);
        }
        //已占用
        return Result.error("用户名已被占用");
    }

    @PostMapping("/login")
    public Result login(@RequestBody Map<String, String> params) {
        String username = params.get("username");
        String password = params.get("password");
        //根据用户名查询User
        CommunityAdmin loginUser = communityAdminService.findByUsername(username);
        //判断是否查询到
        if (loginUser == null) {
            return Result.error("用户名错误");
        }
        //判断密码是否正确
//        if(Md5Util.getMD5String(password).equals(loginUser.getPassword())){

        if (password.equals(loginUser.getPassword())) {
            //状态不是 已通过 不允许登录
            if (!loginUser.getState().equals("已通过")) {
                return Result.error("审核未通过");
            }

            //登录成功
            Map<String, Object> claims = new HashMap<>();
            claims.put("id", loginUser.getId());
            claims.put("username", loginUser.getUsername());
            claims.put("communityId", loginUser.getCommunityId());
            claims.put("state", loginUser.getCommunityId());
            claims.put("userType", 0);

            String token = JwtUtil.genToken(claims);


            return Result.success(token);
        }
        //登录失败
        return Result.error("密码错误");
    }

    @GetMapping("/info")
    public Result<CommunityAdmin> communityAdminInfo() {
        Map<String, Object> map = ThreadLocalUtil.get();
        String username = (String) map.get("username");
        System.out.println(username);
        CommunityAdmin c = communityAdminService.findByUsername(username);
        return Result.success(c);
    }

    @PostMapping("/update")
    public Result update(@RequestBody CommunityAdmin communityAdmin) {
        System.out.println(communityAdmin);
        communityAdminService.update(communityAdmin);
        return Result.success();
    }

    @PostMapping("/updateState")
    public Result updateState(@RequestBody Map<String, String> map) {
        String state = map.get("state");
        String username = map.get("username");
        if (state == null || username == null) {
            return Result.error("信息不全");
        }

        communityAdminService.updateState(state, username);
        return Result.success();
    }

    @PostMapping("/updateCommunityId")
    public Result updateCommunityId(@RequestBody Map<String, Integer> params) {
        Integer communityId = (Integer) params.get("communityId");
        Integer communityAdminId = (Integer) params.get("communityAdminId");

        if (communityAdminId == null || communityId == null) {
            return Result.error("信息不全");
        }
        communityAdminService.updateCommunityId(communityId, communityAdminId);
        return Result.success();
    }

    @PostMapping("/updateAvatar")
    public Result updateAvatar(@RequestBody Map<String, String> params) {
        @URL
        String url = params.get("url");
        System.out.println(url);
        communityAdminService.updateAvatar(url);
        return Result.success();
    }


    @PostMapping("/updatePwd")
    public Result updatePwd(@RequestBody Map<String, String> params) {

        //校验参数
        String oldPwd = params.get("old_pwd");
        String newPwd = params.get("new_pwd");
        String rePwd = params.get("re_pwd");

        if (!StringUtils.hasLength(oldPwd) || !StringUtils.hasLength(newPwd) || !StringUtils.hasLength(rePwd)) {
            return Result.error("缺少必要的参数");
        }

        //校验原密码是否正确

        //调用userservice根据用户拿到原密码，再和old_pwd比较
        Map<String, Object> map = ThreadLocalUtil.get();
        String username = (String) map.get("username");

        CommunityAdmin c = communityAdminService.findByUsername(username);

        if (!Md5Util.getMD5String(oldPwd).equals(c.getPassword())) {
            return Result.error("原密码不正确");
        }

        //new_pwd与re_pwd是否一样
        if (!newPwd.equals(rePwd)) {
            return Result.error("两次密码输入不一致");
        }

        //调用service完成密码更新
        communityAdminService.updatePwd(newPwd);

        return Result.success();
    }

    @GetMapping("/list")
    public Result<PageBean<CommunityAdmin>> listAdminAndCommunity(
            Integer pageNum,    //第几页
            Integer pageSize,   //页面容量
            @RequestParam(required = false) String username,    //非必须
//            @RequestParam(required = false) String province,
//            @RequestParam(required = false) String city,
//            @RequestParam(required = false) String name,
//            @RequestParam(required = false) String districtOrCountry,
            @RequestParam(required = false) String state
    ) {
        PageBean<CommunityAdmin> pb = communityAdminService.list(pageNum, pageSize, username, state);

        return Result.success(pb);
    }

    @GetMapping("/delete")
    public Result delete(Integer id) {
        communityAdminService.delete(id);
        return Result.success();
    }


    @GetMapping("/getInfoById")
    public Result<CommunityAdmin> getInfoById(Integer id){
        CommunityAdmin communityAdmin = communityAdminService.getInfoById(id);
        return Result.success(communityAdmin);
    }
}
